﻿using System.Windows.Automation;
using QuickUIT.Utilities; 

namespace QuickUIT
{
    /// <summary>
    /// Provides configuration options for the automater. 
    /// </summary>
    public class AutomationConfiguration
    {
        private static AutomationConfiguration _current;

        private const int DEFAULT_WINDOW_WAIT_TIME = 30000;
        private const int DEFAULT_OBJECT_SEARCH_TIME = 30000;
        private readonly TreeWalker DEFAULT_TREE_WALKER = TreeWalker.RawViewWalker;

        private int _windowWaitTime = DEFAULT_WINDOW_WAIT_TIME;
        private int _objectSearchTime = DEFAULT_OBJECT_SEARCH_TIME;
        private TreeWalker _treeWalker;

        private AutomationConfiguration()
        {
            _treeWalker = DEFAULT_TREE_WALKER; 
        }

        /// <summary>
        /// Gets the current <see cref="AutomationConfiguration"/>. 
        /// </summary>
        public static AutomationConfiguration Current
        {
            get { return _current ?? (_current = new AutomationConfiguration()); }
        }

        /// <summary>
        /// The time in milliseconds to wait until windows are ready. 
        /// </summary>
        public int WindowWaitTime
        {
            get { return _windowWaitTime; }
            set
            {
                ValidationUtility.ValidateValueGreaterThan(value, 0, "value");
                _windowWaitTime = value; 
            }
        }

        /// <summary>
        /// The time in milliseconds to wait for objects to be found. 
        /// </summary>
        public int ObjectSearchTime
        {
            get { return _objectSearchTime; }
            set
            {
                ValidationUtility.ValidateValueGreaterThan(value, 0, "value");
                _objectSearchTime = value; 
            }
        }

        /// <summary>
        /// Gets and sets the tree walker view (i.e, raw, control, content). 
        /// </summary>
        public TreeWalker TreeWalker
        {
            get { return _treeWalker; }
            set
            {
                ValidationUtility.ValidateArgumentNotNull(value, "value");
                _treeWalker = value; 
            }
        }

        /// <summary>
        /// Resets all the configuration options to their defaults. 
        /// </summary>
        public void Reset()
        {
            _windowWaitTime = DEFAULT_WINDOW_WAIT_TIME;
            _objectSearchTime = DEFAULT_OBJECT_SEARCH_TIME;
            _treeWalker = DEFAULT_TREE_WALKER; 
        }
    }
}
